import request from './request'
import {app} from '../utilities/app'
import {passwordHash} from '../utilities/crypto'

export async function login ({name, password}) {
  const userInfo = await request({
    url: '/login.do',
    method: 'POST',
    body: {
      userName: name,
      pwd: passwordHash(password)
    }
  })
  app.setUser(name)
  app.setToken(userInfo.token)
  app.setUserInfo(userInfo)
  // console.log(userInfo.userType)
  app.setUserType(userInfo.userType)
}

export function logout () {
  request({
    url: '/logout.do',
    method: 'POST'
  }).catch(() => {})
  app.setUser()
  app.setToken()
  app.setUserInfo()
  app.setUserType()
  app.setModules()
  return true
}

